package com.xbongbong.paas.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.enums.LogicalSymbolEnum;
import com.xbongbong.paas.help.ComplexFormatHelp;
import com.xbongbong.paas.help.factory.OperatorFieldHandler;
import com.xbongbong.paas.help.factory.OperatorFieldHandlerFactory;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.pojo.OperationsPojo;
import com.xbongbong.paas.service.BusinessRuleDynamicValueService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.Arith;
import com.xbongbong.paas.toolbox.util.MathUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.pro.businessrule.pojo.BusinessRuleInfoPojo;
import com.xbongbong.pro.businessrule.pojo.dto.OperationDTO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2020/09/29 14:16
 */
@Service("businessRuleDynamicValueService")
public class BusinessRuleDynamicValueServiceImpl implements BusinessRuleDynamicValueService {


    @Resource
    private ComplexFormatHelp complexFormatHelp;
    @Resource
    private OperatorFieldHandlerFactory operatorFieldHandlerFactory;

    @Override
    public Object dynamicValueOfMultiple(OperationsPojo operationsPojo, FieldAttrEntity thisField, FieldAttrEntity targetField, PaasFormDataEntityExt item, JSONObject targetData, OperationDTO operationDTO,
                           List<JSONObject> resultList) throws XbbException {

        if (Objects.equals(LogicalSymbolEnum.EQUAL.getSymbol(), operationsPojo.getSymbol())) {
            JSONObject resultJson = resultList.get(resultList.size()-1);
            Object value = dynamicValue(operationsPojo, thisField, targetField, item, item.getData(), operationDTO, resultJson);
            return value;
        } else {
            String targetAttr = targetField.getAttr();
            Double thisDataResult = 0D;
            Double target = targetData.getDouble(targetAttr) == null ? 0D : targetData.getDouble(targetAttr);
            for (int i = 0; i < resultList.size(); i++) {
                // 当有多个结果时，对这些结果进行累加
                PaasFormDataEntity thisDataEntity = operationDTO.getPaasFormDataEntity();
                Object result = complexFormatHelp.getUpdateResult(thisDataEntity, resultList.get(i), operationsPojo, operationDTO);
                Double thisValue = result == null || StringUtil.isEmpty(result.toString()) ? 0D : Double.valueOf(result.toString());
                thisDataResult = Arith.add(thisDataResult, thisValue);
            }
            // 根据上面的累加结果，之后判断加或者减，由目标表单去加或者减上面的累加结果
            Double result = 0D;
            if (Objects.equals(LogicalSymbolEnum.SUM.getSymbol(), operationsPojo.getSymbol())) {
                result = Arith.add(target, thisDataResult);
            } else if (Objects.equals(LogicalSymbolEnum.LESS.getSymbol(), operationsPojo.getSymbol())) {
                result = Arith.sub(target, thisDataResult);
            }
            Integer accuracy = Objects.equals(1, targetField.getIntegerOnly()) || targetField.getAccuracy() == null  ? 0 : targetField.getAccuracy();
            // 取整或者四舍五入
            BigDecimal resultBig = MathUtil.ceil(result, accuracy);
            return resultBig;
        }

    }

    @Override
    public Object dynamicValue(OperationsPojo operationsPojo, FieldAttrEntity thisField, FieldAttrEntity targetField, PaasFormDataEntityExt item,  JSONObject targetData,
                             OperationDTO operationDTO, JSONObject thisData) throws XbbException {
        String targetAttr = targetField.getAttr();
        PaasFormDataEntity thisDataEntity = operationDTO.getPaasFormDataEntity();
        Object result = complexFormatHelp.getUpdateResult(thisDataEntity, thisData, operationsPojo, operationDTO);
        if (!Objects.equals(LogicalSymbolEnum.EQUAL.getSymbol(), operationsPojo.getSymbol())) {
            operationsPojo.setValue(result);
            BigDecimal bigDecimal = complexFormatHelp.count(targetField, targetData, targetAttr, operationsPojo);
           return bigDecimal;
        }
        return result;
    }

    @Override
    public Object fixedValue(OperationsPojo operationsPojo, FieldAttrEntity targetField, PaasFormDataEntityExt item,  JSONObject targetData, OperationDTO operationDTO) throws XbbException {

        String targetAttr = targetField.getAttr();
        if (Objects.equals(LogicalSymbolEnum.EQUAL.getSymbol(), operationsPojo.getSymbol())) {

            // 固定值的操作
            BusinessRuleInfoPojo businessRuleInfoPojo = new BusinessRuleInfoPojo(operationDTO.getCorpid(), operationDTO.getThisExplain(), operationDTO.getTargetExplain(), operationDTO.getTarget(), operationDTO.getFormId(), operationDTO.getBusinessType(), operationDTO.getSaasMark(), operationDTO.getOperationType(), operationDTO.getFormDataMap());
            OperatorFieldHandler operatorTargetFieldHandler = operatorFieldHandlerFactory.getHandlerFixedValueByFieldType(targetField.getFieldType());
            Object value = operatorTargetFieldHandler.handlerFixedValue(targetField, businessRuleInfoPojo, operationsPojo);
            return value;
        } else {
            BigDecimal bigDecimal = complexFormatHelp.count(targetField, targetData, targetAttr, operationsPojo);
            return bigDecimal;

        }
    }
}
